In this step of the tutorial you first create the prefab that visualizes the item that the user drags, then you set the texture for each item.
The starting point of this tutorial is stored in the <KanziWorkspace>/Tutorials/Drag and drop/Start directory.
The <KanziWorkspace>/Tutorials/Drag and drop/Completed directory contains the completed project of this tutorial.
The starting point project contains the content you need to complete this tutorial:
In this section you create a prefab that you use to visualize the button that the user is dragging. Instead of dragging the button to a new position, you move an instance of this prefab that looks like the dragged button. When the user ends the drag-and-drop gesture, instead of setting the positions of the buttons in the Grid Layout 2D node, you set the order of the data objects that set the button icons.
To create visualization for the button that the user drags:
In this section you create the data objects that you use to set the icons for the buttons.
To set the icons for the buttons:
If you open the tutorial solution in Visual Studio 2017, when asked to retarget the project to the latest Microsoft toolset, click Cancel.
To open the directory of a Kanzi Studio project from Kanzi Studio, select > Open in Windows Explorer.
onProjectLoaded
() function create the data objects and set the data context:virtual void onProjectLoaded() KZ_OVERRIDE { Domain* domain = getDomain(); // Create a data object named Root. m_rootData = make_shared<DataObject>(domain, "Root"); // Create data objects and add them to the Root data object. // Add to each of the data objects a string data object which contains the kzb URL of a texture. // In the Kanzi Studio project you can find the textures in the Library > Materials and Textures > Textures. DataObjectSharedPtr item0 = make_shared<DataObject>(domain, "item0"); m_rootData->addChild(item0); item0->addChild(make_shared<DataObjectString>(domain, "image", "kzb://drag_and_drop/Textures/Navigation")); DataObjectSharedPtr item1 = make_shared<DataObject>(domain, "item1"); m_rootData->addChild(item1); item1->addChild(make_shared<DataObjectString>(domain, "image", "kzb://drag_and_drop/Textures/Phone")); DataObjectSharedPtr item2 = make_shared<DataObject>(domain, "item2"); m_rootData->addChild(item2); item2->addChild(make_shared<DataObjectString>(domain, "image", "kzb://drag_and_drop/Textures/Applications Home")); DataObjectSharedPtr item3 = make_shared<DataObject>(domain, "item3"); m_rootData->addChild(item3); item3->addChild(make_shared<DataObjectString>(domain, "image", "kzb://drag_and_drop/Textures/Sound Loud")); DataObjectSharedPtr item4 = make_shared<DataObject>(domain, "item4"); m_rootData->addChild(item4); item4->addChild(make_shared<DataObjectString>(domain, "image", "kzb://drag_and_drop/Textures/Car Wheel")); // Get the Screen node. ScreenSharedPtr screen = getScreen(); // Set the Data Context property of the Screen node to the Root data object. // By setting the Data Context property you tell your application from which data source it receives data. screen->setProperty(DataContext::DataContextProperty, m_rootData); } private: // Define a member variable for the Root data object. DataObjectSharedPtr m_rootData;
DragAndDrop
class create a function that assigns the icons to the buttons:private: // To assign correct icons to the buttons, set the Data Context property for each button. void updateItems() { // Create an iterator that iterates through the data objects in the Root data object. DataObject::ChildConstIterator dataIt = m_rootData->beginChildren(), endDataIt = m_rootData->endChildren(); // Create an iterator that iterates through the buttons which are // child nodes of the > Grid Layout 2D node. Node2D::ChildConstIterator nodeIt = m_grid->beginChildren(); // Set the Data Context property of each button node to the correct data object. for (; dataIt != endDataIt; dataIt++, nodeIt++) { Node2DSharedPtr itemNode = *nodeIt; DataObjectSharedPtr itemData = *dataIt; itemNode->setProperty(DataContext::DataContextProperty, itemData); } } // Define a member variable for the Grid Layout 2D node. GridLayout2DSharedPtr m_grid; ...
onProjectLoaded
() function call the updateItems()
function: virtual void onProjectLoaded() KZ_OVERRIDE
{
...
// Get the parent Grid Layout 2D node of the node that the user is dragging.
m_grid = screen->lookupNode<GridLayout2D>("#Grid Layout 2D");
updateItems();
}
To learn more about prefabs, see Using prefabs.
To learn more about data sources, see Data sources.
To learn more about aliases in Kanzi, see Using aliases.